package cz.stv;

import czStvDpds.logging.Logging;
import java.io.IOException;
import java.net.*;
import java.nio.ByteBuffer;
import java.util.*;
import java.util.concurrent.LinkedTransferQueue;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Martin Strouhal
 */
public class ThreadSender extends Thread {

    private DatagramSocket datagramSocket;
    public static int SENDING_PORT = /*5467*/ 7876;
    private LinkedTransferQueue<DatagramPacket> queue;
    //private LinkedList<InetAddress> clients = new LinkedList<>();
    private HashMap<Integer, InetAddress> clients = new HashMap<>();
    public static String confirmation = "Confirmation of registration";
    private DatagramPacket datagramPacket;

    public ThreadSender(LinkedTransferQueue q) {
        try {
            datagramSocket = new DatagramSocket();
        } catch (SocketException ex) {
            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
        }
        queue = q;
    }

    @Override
    public void run() {

        //clients.add(InetAddress.getLoopbackAddress());
        while (true) {
            datagramPacket = null;
            try {
                datagramPacket = queue.take();

                //cteni jestli je paket prvni
                byte bajt[] = new byte[datagramPacket.getLength()];
                bajt = datagramPacket.getData();
                String zprava = new String(bajt);
                Scanner scanner = new Scanner(zprava);
                scanner.useDelimiter("-");
                datagramPacket.setPort(SENDING_PORT);
                if (scanner.hasNextInt()) {
                    if (scanner.nextInt() == 00) {
                        
                        /*if (!clients.containsValue(datagramPacket.getAddress())) {
                            System.out.println("Novy klient"+datagramPacket.getAddress());
                            clients.put(clients.size() + 1, datagramPacket.getAddress());
                            System.out.println("klient pridan do seznamu" + datagramPacket.getAddress());
                            
                            String nick = scanner.next();
                            String data = "00-"+nick+"-"+clients.size()+"-";
                            datagramPacket.setData(data.getBytes());
                            setAdressAndSend();
                        }  */
                        
                            datagramPacket.setData("01-01".getBytes());
                            
                            send(datagramPacket);
                            Logging.getMainLogger().log(Level.SEVERE, "SERVER:Odeslan paket");

                        

                    } else if (scanner.nextInt() == 11) {
                        String need = new String();
                        zprava = zprava + clients.size() + "-";
                        System.out.println("Pohyb:"+zprava);                        
                        datagramPacket.setData(zprava.getBytes());
                        setAdressAndSend();

                    } else {
                    }
                }
            } catch (InterruptedException ex) {
                Logger.getLogger(ThreadSender.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void setAdressAndSend() {       
        
        for (int i = 0; i < clients.size(); i++) {
            // System.out.println("datagramPacket.getaddress " + datagramPacket.getAddress() + "      clients.get(i) " + clients.get(i));
            if (datagramPacket.getAddress() == null || !datagramPacket.getAddress().equals(clients.get(i))) {
                datagramPacket.setAddress(clients.get(i));
                send(datagramPacket);

            }
        }

    }

    public void send(DatagramPacket p) {
        try {
            datagramSocket.send(p);
            
        } catch (IOException ex) {
            Logger.getLogger(ThreadSender.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
